package com.example.pan.ui.main

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.pan.R
import com.example.pan.aria2.Aria2Service
import com.example.pan.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.hi.dhl.binding.viewbind
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.ExperimentalCoroutinesApi


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private val mainViewModel: MainViewModel by viewModels()
    private lateinit var mBinder: Aria2Service.Aria2Binder
    private val binding: ActivityMainBinding by viewbind()

    private val topLevelDestinationIds = setOf(
        R.id.navigation_home,
        R.id.navigation_drive,
        R.id.navigation_download,
        R.id.navigation_settings
    )
    private val appBarConfiguration by lazy { AppBarConfiguration(topLevelDestinationIds) }

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            mBinder = service as Aria2Service.Aria2Binder
        }

        override fun onServiceDisconnected(name: ComponentName?) {

        }

    }

    @ExperimentalCoroutinesApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initService()
        initViewAndListener()
        //
        // registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
        //     if (it.values.contains(false)) {
        //         Toast.makeText(applicationContext, "权限申请失败", Toast.LENGTH_SHORT).show()
        //     } else {
        //         val aria2Service = Intent(this, Aria2Service::class.java)
        //         startService(aria2Service)
        //         bindService(aria2Service, connection, Context.BIND_AUTO_CREATE)
        //     }
        // }.launch(
        //     arrayOf(
        //         Manifest.permission.WRITE_EXTERNAL_STORAGE,
        //         Manifest.permission.READ_EXTERNAL_STORAGE
        //     )
        // )

    }

    private fun initService() {
        val aria2Service = Intent(this, Aria2Service::class.java)
        bindService(aria2Service, connection, Context.BIND_AUTO_CREATE)
    }

    @ExperimentalCoroutinesApi
    private fun initViewAndListener() = with(binding) {
        setSupportActionBar(toolbar)
        mainViewModel.initThemeMode()
        val navView: BottomNavigationView = binding.navView
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
        val navController = navHostFragment.navController
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
        navController.addOnDestinationChangedListener { _, destination, _ ->
            if (topLevelDestinationIds.contains(destination.id)) {
                binding.navView.visibility = View.VISIBLE
            } else {
                binding.navView.visibility = View.GONE
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unbindService(connection)
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        return navController.navigateUp(appBarConfiguration)
                || super.onSupportNavigateUp()
    }
}